package com.miniserver.util;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.net.SyslogAppender;
/**
 * LogUtil 记录不同等级的信息到不同的文件中
 * @author zzlt104
 *
 */
public class LogUtil {
	private Logger loggerDebug = null;
	private Logger loggerError = null;
	private Logger loggerWarn = null;
	private Logger loggerInfo = null;
	private Logger loggerUserTrace = null;
	private Logger loggerPaySuccess = null;
	private Logger loggerPayWarn = null;
	private Logger loggerCostlog = null;
	private  String o_message = null;
	
	private static class PaySuccessLevel extends Level {
		  private static final long serialVersionUID =-1L;

		  private PaySuccessLevel(int level, String name, int sysLogLevel) {
		   super(level, name, sysLogLevel);
		  }
		 }
	private static class PayWarnLevel extends Level {
		  private static final long serialVersionUID =-1L;

		  private PayWarnLevel(int level, String name, int sysLogLevel) {
		   super(level, name, sysLogLevel);
		  }
		 }
	
	private static final Level PAYSUCCESS_LOG_LEVEL = new PaySuccessLevel(
			   Level.INFO_INT+50, "paysuccess", SyslogAppender.LOG_LOCAL0);
	private static final Level PAYWARN_LOG_LEVEL = new PayWarnLevel(
			   Level.ERROR_INT+50, "paywarn", SyslogAppender.LOG_LOCAL0);
	private static final Level COST_LOG_LEVEL = new PaySuccessLevel(
			Level.INFO_INT+100, "costlog", SyslogAppender.LOG_LOCAL0);
	
	private LogUtil(String objMessage) {
		this.o_message = objMessage;
	}

	public static LogUtil getLogUtil(Object message) {
		String objMessage = message.toString();
		return new LogUtil(objMessage);
	}

	public void debug(Object message) {
		if (loggerDebug == null)
		{
			loggerDebug = Logger.getLogger("debugfile");		
			loggerDebug .setLevel(Level.DEBUG);
		}
		if (this.o_message==null)
			return;
		loggerDebug.debug("[" + o_message + "] - " + DKeyDesEncrypt.encryptLogString((String)message));
	}

	public void error(Object message) {
		if (loggerError == null)
		{
			loggerError = Logger.getLogger("errorfile");			
			loggerError .setLevel(Level.ERROR);
		}
		if (this.o_message==null)
			return;
		loggerError.error("[" + o_message + "] - " + DKeyDesEncrypt.encryptLogString((String)message));
	}

	public void info(Object message) {
		if (loggerInfo == null)
		{
			loggerInfo = Logger.getLogger("infofile");			
			loggerInfo .setLevel(Level.INFO);
		}
		if (this.o_message==null)
			return;
		loggerInfo.info("[" + o_message + "] - " + DKeyDesEncrypt.encryptLogString((String)message));
	}

	public void warn(Object message) {
		if (loggerWarn == null)
		{
			loggerWarn = Logger.getLogger("warnfile");		
			loggerDebug .setLevel(Level.WARN);
		}
		if (this.o_message==null)
			return;
		loggerWarn.warn("[" + o_message + "] - " + DKeyDesEncrypt.encryptLogString((String)message));
	}

	public void UserTrace(Object message) {
		if (loggerUserTrace == null)
		{
			loggerUserTrace = Logger.getLogger("usertrace");		
			loggerUserTrace.setLevel(Level.TRACE);
		}
		if (this.o_message==null)
			return;
		loggerUserTrace.trace("[" + o_message + "] - " + DKeyDesEncrypt.encryptLogString((String)message));
	}

	public void error(String message, Exception ex) {
		if (loggerError == null)
		{
			loggerError = Logger.getLogger("errorfile");		
			loggerError .setLevel(Level.ERROR);		
		}
		if (this.o_message==null)
			return;
		loggerError.error("[" + o_message + "] - " + DKeyDesEncrypt.encryptLogString((String)message+ " : "+ex.getMessage()));
	}
	
	public void paySuccess(Object message){
		if (loggerPaySuccess == null)
		{
			loggerPaySuccess = Logger.getLogger("paysuccess");		
			loggerPaySuccess.setLevel(PAYSUCCESS_LOG_LEVEL);			
		}
		if (this.o_message==null)
			return;
		loggerPaySuccess.log(PAYSUCCESS_LOG_LEVEL,"[" + o_message + "] - " + DKeyDesEncrypt.encryptLogString((String)message));
	}
	
	public void payWarn(Object message){
		if (loggerPayWarn == null)
		{
			loggerPayWarn = Logger.getLogger("paywarn");		
			loggerPayWarn.setLevel(PAYWARN_LOG_LEVEL);			
		}
		if (this.o_message==null)
			return;
		loggerPayWarn.log(PAYWARN_LOG_LEVEL,"[" + o_message + "] - " + DKeyDesEncrypt.encryptLogString((String)message));
	}
	
	public void costlog(Object message) {
		if (loggerCostlog == null)
		{
			loggerCostlog = Logger.getLogger("costlog");		
			loggerCostlog.setLevel(COST_LOG_LEVEL);			
		}
		if (this.o_message==null)
			return;
		loggerCostlog.log(COST_LOG_LEVEL,"[" + o_message + "] - " + DKeyDesEncrypt.encryptLogString((String)message));
	}
}
